<?php

namespace IDock\Insurance\AICS\Response;

use IDock\Http\Response;
use IDock\Printer\UploadFilePrinter;
use IDock\Printer\ValidateCarCodePrinter;

class ValidateCarResponse extends AllAICSResponse
{

    public function resolve(): Response
    {
        $body = $this->getBody();

        $body = str_replace(['&lt;','&gt;',"'","\r\n"],['<','>','"',''],$body);
        $start = strpos($body,'<response>');
        $end = strpos($body,'</response>');
        $body = substr($body,$start + 10,intval($end - $start -10));

        if (!$body) {
            $this->setError('数据格式错误');
            return $this;
        }

        libxml_disable_entity_loader(false);

        $body = simplexml_load_string($body);
        $body = $this->objectToArray($body);

        $errorCode = $body['code'] ?? '';
        $message = $body['responseMessage'] ?? '未知错误';
        $this->setBody($body);
        if ($errorCode !== 'E0000') {
            $this->setError($message);
            return $this;
        }

        return $this;
    }
    public function printerInit(): Response
    {
        $this->printer = new ValidateCarCodePrinter();
        $this->printer->code = $this->get('vhlCode');

        return $this;
    }
}
